package com.medical.controller;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.medical.HttpTools.HttpRequest;
import com.medical.po.HosplogininfoCustom;
import com.medical.service.HosplogininfoService;

@Controller  
//@RequestMapping(value = "/web", method = {RequestMethod.GET, RequestMethod.POST}) 
@RequestMapping(value="/Main")
/**
 * 
 * @author LTT-AH-ZY
 * @date 创建时间：2017年7月24日 下午5:07:33 
 * @version 1.0
 * @parameter 参数及其意义
 * @return 返回值
 * @throws 异常类及抛出条件
 */
public class MainFrameContorller {
	/**
	 * 注入HosplogininfoService服务
	 */
	@Autowired
	private HosplogininfoService hosplogininfoService ;
	/**
	 * 初始化主界面
	 * @return
	 */
	@RequestMapping(value = "/main")  
    public String main() {  
        return "MainFrame/MainFrame/MainFrame";  
    } 
	/**
	 * 显示主界面view，初始化view的PageIndex
	 * @return
	 */
	@RequestMapping(value = "/pageindex")  
    public ModelAndView PageIndex(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String hosploginphone = (String)session.getAttribute("hosploginphone");
		ModelAndView model = new ModelAndView();
		model.addObject("hosploginphone", hosploginphone);
		model.setViewName("MainFrame/MainFrame/PageIndex");
        return model;
    }
	/**
	 * 初始化显示修改密码界面view
	 * @return
	 */
	@RequestMapping(value="/changePas")
	public ModelAndView change(){
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/MainFrame/ChangePas");
		return model;
	}
	/**
	 * 修改密码界面的form提交事件submit
	 * @param hosplogininfoCustom
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/changePasSubmit")
	public @ResponseBody int changePasSubmit(@RequestBody HosplogininfoCustom hosplogininfoCustom,HttpServletRequest request,HttpServletResponse response, HttpSession session)throws Exception{
		response.setContentType("application/json; charset=utf-8");
		try {
			//response.setContentType("application/json; charset=utf-8");  
			Integer hosploginid = hosplogininfoCustom.getHosploginid();
			String hosploginpwd = hosplogininfoCustom.getHosploginpwd();
			System.out.println("输出接收到的hosploginid:"+hosploginid);
			System.out.println("输出接收到的hospitalpassword:"+hosploginpwd);
			hosplogininfoCustom.setHosploginpwd(hosploginpwd);
			hosplogininfoCustom.setHosploginid(hosploginid);
			int result = hosplogininfoService.updateHospLogin(hosplogininfoCustom);
			System.out.println("输出查询结果"+result);
			if(result==1){
				session.setAttribute("password", hosplogininfoCustom.getHosploginpwd());
			}
			return result;
		} catch (Exception e) {
			System.out.println("输出异常信息"+e.getMessage());
			return -1;
		}
	}
	/**
	 * 修改头像界面初始化
	 * @return
	 */
	@RequestMapping(value="changePix")
	public ModelAndView changePix(){
		ModelAndView model  = new ModelAndView();
		model.setViewName("MainFrame/MainFrame/ChangePix");
		return model;
	}
	/**
	 * 图片上传提交类，接收前台传过来的image，base64字节流
	 * @param image
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/changePixSubmit")
	public @ResponseBody int chagePixSubmit(@RequestBody String image)throws Exception{
		System.out.println("输出接收到的image是："+image);
		System.out.println("测试位置。。。。");
		return 0;
	}
	/**
	 * 获取当前未读信息的数量
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getmessagetotal")
	public @ResponseBody Map<String,Object> getmessagetotal(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		
		Map<String,Object> result = new HashMap<>();
		Integer number=0;
		String jsoncode = "";
		String jsonmsg = "";
		try {
			JSONObject json = HttpRequest.sendPost("getunreadnotificationnum", "hosploginid="+hosploginid);
			jsoncode = json.get("code").toString();
			jsonmsg = json.get("msg").toString();
			try {
				if(json.get("data").toString()!="null") {
					JSONObject job = json.getJSONObject("data");
					number = job.getInt("number");
				}
			} catch (Exception e) {
				System.out.println("出错了，出错信息是："+e.getMessage());
				jsoncode = "-200";
				jsonmsg = "数据解析出错";
			}
		} catch (Exception e) {
			System.out.println("出错了，出错信息是："+e.getMessage());
			jsoncode = "-100";
			jsonmsg = "输出传输出错";
		}
		result.put("code", jsoncode);
		result.put("msg", jsonmsg);
		result.put("number", number);
		return result;
	}
}
